//
//  TradeMainView.m
//  bitcoin
//
//  Created by  旺链 on 2018/5/31.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "TradeMainView.h"
#import "PriceTableViewCell.h"
#import "TradeChangeView.h"
#import "CALayer+XibConfiguration.h"

#define cellHeight 30

@interface TradeMainView()<UITableViewDelegate,UITableViewDataSource,HWDownSelectedViewDelegate>
{
    UILabel *newPriceLb;

}
@property(strong,nonatomic)NSString *bName;
@property(strong,nonatomic)TradeChangeView *changePriceView;


@end
@implementation TradeMainView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib
{
    [super awakeFromNib];
    self.changePriceView = [[[NSBundle mainBundle]loadNibNamed:@"TradeChangeView" owner:self options:nil]firstObject];
    self.changePriceView.isMarkertPrice = NO;
    self.changePriceView.isSell = NO;
    
    [self.changeView addSubview:self.changePriceView];
    [self.changePriceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.top.right.equalTo(0);
        make.height.equalTo(235);
    }];
    
    _chooseView.placeholder = @"限价";
    _chooseView.listArray = @[@"限价",@"市价"];
    _chooseView.textColor = [UIColor colorWithHexString:@"#999999"];
    _chooseView.delegate = self;
    
    self.table.delegate = self;
    self.table.dataSource = self;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.table registerNib:[UINib nibWithNibName:@"PriceTableViewCell" bundle:nil] forCellReuseIdentifier:@"PriceTableViewCell"];
    
    self.selectBtn = self.buyInBtn;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeUI:) name:@"changeUI" object:nil];
    
    if ([[UserDefaults objectForKey:K_PRICE]isEqualToString:@"1"]) {
        self.bName = @"CNY";
    }else if ([[UserDefaults objectForKey:K_PRICE]isEqualToString:@"2"]){
        self.bName = @"USD";
    }else{
        self.bName = @"AUD";
    }
}

- (IBAction)loginOrBuyClick:(id)sender {
    if (self.loginOrBuy) {
        
        if (IsLogin) {
            NSDictionary *para =@{@"price":self.changePriceView.isMarkertPrice?@"0":self.changePriceView.inputPrice,@"amount":self.changePriceView.countTf.text,@"businessType":self.changePriceView.isSell?@"2":@"1",@"txType":self.changePriceView.isMarkertPrice?@"2":@"1"};
             self.loginOrBuy(para);
        }else{
             self.loginOrBuy(nil);
        }
       
    }
}
- (IBAction)allorderClick:(id)sender {
    if (self.allOrder) {
        self.allOrder();
    }
}

- (IBAction)buyClick:(UIButton*)sender {
    
    self.selectBtn = sender;
    if (sender == self.buyInBtn) {
        //买入
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeUI" object:@"0"];
        
    }else{
        //卖出
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeUI" object:@"1"];
        
    }
}
- (void)changeUI:(NSNotification* )noti
{
    if ([noti.object isEqualToString:@"0"]) {
        //买入按钮
        [self.buyInBtn setTitleColor:[UIColor colorWithHexString:@"#2CB25A"] forState:UIControlStateNormal];
        self.buyInBtn.layer.borderUIColor = [UIColor colorWithHexString:@"#2CB25A"];
        
        //卖出按钮
        [self.soldOutBtn setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        self.soldOutBtn.layer.borderUIColor = [UIColor colorWithHexString:@"#999999"];
        
        //买入登录按钮
        [self.loginOrBuyBtn setBackgroundColor:[UIColor colorWithHexString:@"#2CB25A"]];
        if (IsLogin) {
            [self.loginOrBuyBtn setTitle:[NSString stringWithFormat:@"买入%@",self.tradeCurrency] forState:UIControlStateNormal];
        }else{
            [self.loginOrBuyBtn setTitle:@"登录" forState:UIControlStateNormal];
        }
        
        
        //滑杆
        self.changePriceView.zzslider.minimumTrackTintColor = [UIColor colorWithHexString:@"#2CB25A"];
        [self.changePriceView.zzslider setThumbImage:[UIImage imageNamed:@"green-icon"] forState:UIControlStateNormal];
        
        //最新成交价
        self->newPriceLb.textColor =[UIColor colorWithHexString:@"#2CB25A"];
        
        self.changePriceView.isSell = NO;
    }else{
        [self.buyInBtn setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        self.buyInBtn.layer.borderUIColor = [UIColor colorWithHexString:@"#999999"];
        
        [self.soldOutBtn setTitleColor:[UIColor colorWithHexString:@"#E1454F"] forState:UIControlStateNormal];
        self.soldOutBtn.layer.borderUIColor = [UIColor colorWithHexString:@"#E1454F"];
        
        [self.loginOrBuyBtn setBackgroundColor:[UIColor colorWithHexString:@"#E1454F"]];
        
        if (IsLogin) {
            [self.loginOrBuyBtn setTitle:[NSString stringWithFormat:@"卖出%@",self.tradeCurrency] forState:UIControlStateNormal];
        }else{
            [self.loginOrBuyBtn setTitle:@"登录" forState:UIControlStateNormal];
        }
        
        
        self.changePriceView.zzslider.minimumTrackTintColor = [UIColor colorWithHexString:@"#E1454F"];
        [self.changePriceView.zzslider setThumbImage:[UIImage imageNamed:@"red-icon"] forState:UIControlStateNormal];
        
        self->newPriceLb.textColor =[UIColor colorWithHexString:@"#E1454F"];
        
        self.changePriceView.isSell = YES;
        
    }
}
- (void)setBName:(NSString *)bName
{
    _bName = bName;
    self.changePriceView.bName = bName;
}
- (void)setBId:(NSString *)bId
{
    _bId = bId;
    self.changePriceView.bId = bId;
}
-(void)setAssetsData:(NSDictionary *)assetsData
{
    _assetsData = assetsData;
    
    self.changePriceView.assetsData = assetsData;
}
- (void)setListArr:(NSArray *)listArr
{
    if ([[UserDefaults objectForKey:K_PRICE]isEqualToString:@"1"]) {
        self.bName = @"CNY";
    }else if ([[UserDefaults objectForKey:K_PRICE]isEqualToString:@"2"]){
        self.bName = @"USD";
    }else{
        self.bName = @"AUD";
    }
    
    
    _listArr = listArr;
    [self.table reloadData];
    
    self.changePriceView.sellFirstPrice = [listArr[0]lastObject][@"price"];
    self.changePriceView.buyFirstPrice = [listArr[1]firstObject][@"price"];
}
- (void)setTradeCurrency:(NSString *)tradeCurrency
{
    _tradeCurrency = tradeCurrency;
    //self.changePriceView.tradeCurrency = _tradeCurrency;
    
    if (IsLogin) {
    
    if (self.selectBtn == self.buyInBtn) {
        
        [self.loginOrBuyBtn setTitle:[NSString stringWithFormat:@"买入%@",_tradeCurrency] forState:UIControlStateNormal];
    }else{
        [self.loginOrBuyBtn setTitle:[NSString stringWithFormat:@"卖出%@",_tradeCurrency] forState:UIControlStateNormal];
    }
    }
}
- (void)setPayCurrency:(NSString *)payCurrency
{
    _payCurrency = payCurrency;
    //self.changePriceView.payCurrency = self.payCurrency;
}
- (void)setCurrencyRelation:(NSString *)currencyRelation
{
    _currencyRelation = currencyRelation;
    //设置单位
    self.changePriceView.currencyRelation = currencyRelation;
    
    NSArray *arr = [currencyRelation componentsSeparatedByString:@"/"];
    self.tradeCurrency = arr[0];
    self.payCurrency = arr[1];
}
- (void)setPrecisionDic:(NSDictionary *)precisionDic
{
    _precisionDic = precisionDic;
    self.changePriceView.precisionDic = precisionDic;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PriceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PriceTableViewCell"];
    
    NSArray *arr = self.listArr[indexPath.section];
    NSDictionary *dic = arr[indexPath.row];
    [cell config:dic];

    
    //设置选中背景色
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    
    
    if (indexPath.section == 0) {
        
        cell.numLb.text = [NSString stringWithFormat:@"%ld",5-indexPath.row];
        cell.priceLb.textColor = [UIColor colorWithHexString:@"#E1454F"];
        
        cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHexString:@"#43273A"];
    }else{
        cell.numLb.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        cell.priceLb.textColor = [UIColor colorWithHexString:@"#2CB25A"];
        
        cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHexString:@"#1E2F39"];
    }
    
    
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return cellHeight;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        PriceTableViewCell *cView = [[[NSBundle mainBundle]loadNibNamed:@"PriceTableViewCell" owner:self options:nil]firstObject];
        cView.frame = CGRectMake(0, 0, self.mm_width, 36);
        cView.numLb.text = @"盘口";
        cView.priceLb.text = @"价格";
        cView.countLb.text = @"数量";
        return cView;
    }else{
        
        NSDictionary *dic = [self.listArr lastObject];
        
        if ([dic isKindOfClass:[NSNull class]]) {
            return nil;
        }
        NSString *price = @"0";
        NSString *legalQuantity = @"0";
        if (![dic isEqual:@(0)]) {
            price =dic[@"price"];
            legalQuantity =dic[@"legalQuantity"];
        }
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.mm_width, 36)];
        
        newPriceLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.mm_width, 26)];
        newPriceLb.font = [UIFont systemFontOfSize:14];
        newPriceLb.textColor = [UIColor colorWithHexString:@"#2CB25A"];
        newPriceLb.text = price;
        [view addSubview:newPriceLb];
        
        UILabel *l = [[UILabel alloc]initWithFrame:CGRectMake(0, newPriceLb.mm_bottom, self.mm_width, 10)];
        l.font = [UIFont systemFontOfSize:10];
        l.textColor = [UIColor colorWithHexString:@"#999999"];
        l.text = [NSString stringWithFormat:@"≈ %@ %@",legalQuantity,self.bName];
        [view addSubview:l];
        
        return view;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return cellHeight*1.5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arr = self.listArr[indexPath.section];
    NSDictionary *dic = arr[indexPath.row];
    self.changePriceView.inputPrice = dic[@"price"];
    [self endEditing:YES];
}

- (void)downSelectedView:(HWDownSelectedView *)selectedView didSelectedAtIndex:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        self.changePriceView.isMarkertPrice = NO;
    }else{
        self.changePriceView.isMarkertPrice = YES;
    }
}

@end
